package cn.sun.user.controller;

import cn.sun.basic.util.AjaxResult;
import cn.sun.user.dto.LoginDto;
import cn.sun.user.service.ILoginInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("login")
public class LoginController {
    @Autowired
    private ILoginInfoService loginInfoService;
    @PostMapping("/phone")
    public AjaxResult phoneLogin(@RequestBody LoginDto loginDto){
        try {
            return loginInfoService.phoneLogin(loginDto);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }

    @PostMapping("/wechat")
    public AjaxResult wechatLogin(@RequestBody Map<String,String> params){
        try {
            return loginInfoService.wechatLogin(params);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统错误！"+e.getMessage());
        }

    }

    @PostMapping("/binder/wechat")
    public AjaxResult binderWechat(@RequestBody Map<String,String> params){
        try {
            return loginInfoService.binderWechat(params);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统错误！"+e.getMessage());
        }

    }

    @PostMapping("/logout")
    public AjaxResult logout(@RequestBody String token){
        try {
            loginInfoService.logout(token);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统错误！"+e.getMessage());
        }

    }

}
